//
//  YWMaterialDetailAndCertainHeadTableViewCell.m
//  maintain
//
//  Created by 烟雨云渊 on 16/9/23.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWMaterialDetailAndCertainHeadTableViewCell.h"
#import "YWMacro.h"
#import "Masonry.h"
#import "UIColor+CustomColors.h"
#import "UIImageView+WebCache.h"
#import "YWCommonTool.h"

#import "YWMaterialApply.h"
#import "YWMaterialStockOut.h"
#import "YWMaterialReturn.h"
#import "YWHandler.h"
#import "YYLabel.h"

@interface YWMaterialDetailAndCertainHeadTableViewCell()
/** 单名 */
@property (nonatomic, strong) UIView *letfLine;
@property (nonatomic, strong) UIView *rightLine;
@property (nonatomic, strong) YYLabel *materialNameLabel;
/**  申请单号 */
@property (nonatomic, strong) YYLabel *applyNumbersLabel;
/** 申请人名称 */
@property (nonatomic, strong) YYLabel *applyNameLabel;
/**  接收人 */
@property (nonatomic, strong) YYLabel *receivedManNameLabel;
/**  抄送人 */
//@property (nonatomic, strong) YYLabel * copiedManLabel;
/**  申请时间 */
@property (nonatomic, strong) YYLabel *applyDateLabel;
/** 用途 */
@property (nonatomic, strong) YYLabel * noteLabel;
@end

@implementation YWMaterialDetailAndCertainHeadTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setUpSubviews];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)setUpSubviews{
    /**
     *  ------ 单名 -------
     */
    [self.materialNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.contentView);
        make.top.mas_equalTo(self.contentView).offset(8);
    }];
    [self.letfLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView).offset(8);
        make.right.mas_equalTo(_materialNameLabel.mas_left).offset(-8);
    }];
    [self.rightLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(@[_materialNameLabel,_letfLine]);
        make.left.mas_equalTo(_materialNameLabel.mas_right).offset(8);
        make.right.mas_equalTo(self.contentView).offset(-8);
        make.height.mas_equalTo(@[@1,_letfLine]);
    }];
    /**  申请单号 */
    [self.applyNumbersLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_materialNameLabel.mas_bottom).offset(10);
        make.left.mas_equalTo(self.contentView).offset(8);
        make.right.mas_equalTo(self.contentView).offset(-8);
    }];
    /* 申请人 */
    [self.applyNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_applyNumbersLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(self.contentView).offset(8);
        make.right.mas_equalTo(self.contentView).offset(-8);
    }];
    /* 接收人 */
    [self.receivedManNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_applyNameLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(self.contentView).offset(8);
        make.right.mas_equalTo(self.contentView).offset(-8);
    }];
    /* 抄送人 */
//    [self.copiedManLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(_receivedManNameLabel.mas_bottom).offset(6);
//        make.left.mas_equalTo(self.contentView).offset(8);
//        make.right.mas_equalTo(self.contentView).offset(-8);
//    }];
    /* 申请日期 */
    [self.applyDateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_receivedManNameLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(self.contentView).offset(8);
        make.right.mas_equalTo(self.contentView).offset(-8);
    }];
    /* 用途 */
    [self.noteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_applyDateLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(self.contentView).offset(8);
        make.right.mas_equalTo(self.contentView).offset(-8);
        make.bottom.mas_equalTo(self.contentView).offset(-6);
    }];
}

//- (void)layoutSubviews{
//    [super layoutSubviews];
//    
//}

- (void)setApply:(YWMaterialApplyViewModel *)apply
{
    _apply = apply;
    _materialNameLabel.text = @"申请单";
    
    _applyNumbersLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"申请单号: " firstColor:[UIColor customTextGrayBlackColor] secondText:_apply.applySequence secondColor:[UIColor customYellowColor] font:kContentFont];
    NSString* applierName =_apply.creator.userName?:@"未知用户";
    _applyNameLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"申请人: " firstColor:[UIColor customTextGrayBlackColor] secondText:applierName secondColor:[UIColor customYellowColor] font:kContentFont];
    NSMutableString * receipientString = [NSMutableString string];
    [_apply.receivers enumerateObjectsUsingBlock:^(YWHandler *  _Nonnull handler, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx!=0) {
            [receipientString appendString:@","];
        }
        [receipientString appendString:[NSString stringWithFormat:@"%@",handler.userName]];
    }];
    
    _receivedManNameLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"接收人: " firstColor:[UIColor customTextGrayBlackColor] secondText:receipientString secondColor:[UIColor customYellowColor] font:kContentFont];
//    NSMutableString * copierString = [NSMutableString string];
//    [_apply.copies enumerateObjectsUsingBlock:^(YWHandler *  _Nonnull handler, NSUInteger idx, BOOL * _Nonnull stop) {
//        if (idx!=0) {
//            [copierString appendString:@","];
//        }
//        [copierString appendString:[NSString stringWithFormat:@"%@",handler.userName?:@"无"]];
//    }];
//    if (copierString.length>0) {
//        [_copiedManLabel labelText:@"抄送人: " withAttributedText:copierString andAttributedColor:[UIColor customYellowColor]];
//    }else{
//        [_copiedManLabel labelText:@"抄送人: " withAttributedText:@"无" andAttributedColor:[UIColor customYellowColor]];
//    }
    
    NSString* applyTime =[YWCommonTool showTimeWithNormalFormatter:_apply.applyTime /1000];
    _applyDateLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"申请时间: " firstColor:[UIColor customTextGrayColor] secondText:applyTime secondColor:[UIColor blackColor] font:kContentFont];
    if (_apply.note.length>0) {
        _noteLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"用途: " firstColor:[UIColor customTextGrayBlackColor] secondText:_apply.note secondColor:[UIColor blackColor] font:kContentFont];
    }else{
        _noteLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"用途: " firstColor:[UIColor customTextGrayBlackColor] secondText:@"无" secondColor:[UIColor blackColor] font:kContentFont];
    }
    
    self.noteLabel.preferredMaxLayoutWidth = self.noteLabel.frame.size.width;
    self.receivedManNameLabel.preferredMaxLayoutWidth = self.receivedManNameLabel.frame.size.width;
}


- (void)setReceipient:(YWMaterialStockOut *)receipient
{
    _receipient = receipient;
    _materialNameLabel.text = @"领用单";
    _applyNumbersLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"领用单号: " firstColor:[UIColor customTextGrayBlackColor] secondText:_receipient.stockOutSequence secondColor:[UIColor customYellowColor] font:kContentFont];
    NSString* receiverName =_receipient.receiver.userName?:@"未知用户";
    _applyNameLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"领用人: " firstColor:[UIColor customTextGrayBlackColor] secondText: receiverName secondColor:[UIColor customYellowColor] font:kContentFont];
    //仓管
    _receivedManNameLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"发放人: " firstColor:[UIColor customTextGrayBlackColor] secondText:_receipient.creator.userName?:@"未知用户" secondColor:[UIColor customYellowColor] font:kContentFont];
    [_applyDateLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_receivedManNameLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(self.contentView).offset(8);
        make.right.mas_equalTo(self.contentView).offset(-8);
    }];
    
    NSString*stockOutTime =[YWCommonTool showTimeWithNormalFormatter:_receipient.stockOutTime /1000];
    _applyDateLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"领用时间: " firstColor:[UIColor customTextGrayColor] secondText:stockOutTime secondColor:[UIColor blackColor] font:kContentFont];
//    if (_receipient.note.length>0) {
//        [_noteLabel labelText:@"用途: " withAttributedText:_receipient.note andAttributedColor:[UIColor customYellowColor]];
//    }else{
//        [_noteLabel labelText:@"用途: " withAttributedText:@"无" andAttributedColor:[UIColor customYellowColor]];
//    }
    
    self.noteLabel.preferredMaxLayoutWidth = self.noteLabel.frame.size.width;
    self.receivedManNameLabel.preferredMaxLayoutWidth = self.receivedManNameLabel.frame.size.width;
}

- (void)setReturnBack:(YWMaterialReturn *)returnBack
{
    _returnBack = returnBack;
    _materialNameLabel.text = @"归还单";
    _applyNumbersLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"归还单号: " firstColor:[UIColor customTextGrayBlackColor] secondText:_returnBack.returnSequence secondColor:[UIColor customYellowColor] font:kContentFont];
    NSString* returnerName = _returnBack.returner.userName?:@"未知用户";
    _applyNameLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"归还人: " firstColor:[UIColor customTextGrayBlackColor] secondText:returnerName secondColor:[UIColor customYellowColor] font:kContentFont];
    //仓管
    _receivedManNameLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"接收人: " firstColor:[UIColor customTextGrayBlackColor] secondText:_returnBack.creator.userName?:@"未知用户" secondColor:[UIColor customYellowColor] font:kContentFont];
    [_applyDateLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_receivedManNameLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(self.contentView).offset(8);
        make.right.mas_equalTo(self.contentView).offset(-8);
    }];
    NSString*returnTime = [YWCommonTool showTimeWithNormalFormatter:_returnBack.returnTime/1000];
    _applyDateLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"归还时间: " firstColor:[UIColor customTextGrayColor] secondText:returnTime secondColor:[UIColor blackColor] font:kContentFont];
//    if (_returnBack.note.length>0) {
//        [_noteLabel labelText:@"用途: " withAttributedText:_returnBack.note andAttributedColor:[UIColor customYellowColor]];
//    }else{
//        [_noteLabel labelText:@"用途: " withAttributedText:@"无" andAttributedColor:[UIColor customYellowColor]];
//    }
    
    
    self.noteLabel.preferredMaxLayoutWidth = self.noteLabel.frame.size.width;
    self.receivedManNameLabel.preferredMaxLayoutWidth = self.receivedManNameLabel.frame.size.width;
}


- (YYLabel *)materialNameLabel{
    if (!_materialNameLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayBlackColor];
        _materialNameLabel = label;
        [self.contentView addSubview:_materialNameLabel];
    }
    return _materialNameLabel;
}

- (UIView *)letfLine{
    if (!_letfLine) {
        _letfLine = [UIView new];
        _letfLine.backgroundColor = [UIColor customLineColor];
        [self.contentView addSubview:_letfLine];
    }
    return _letfLine;
}

- (UIView *)rightLine{
    if (!_rightLine) {
        _rightLine = [UIView new];
        _rightLine.backgroundColor = [UIColor customLineColor];
        [self.contentView addSubview:_rightLine];
    }
    return _rightLine;
}

- (YYLabel *)applyNumbersLabel{
    if (!_applyNumbersLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        _applyNumbersLabel = label;
        [self.contentView addSubview:_applyNumbersLabel];
    }
    return _applyNumbersLabel;
}

- (YYLabel *)applyNameLabel{
    if (!_applyNameLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        _applyNameLabel = label;
        [self.contentView addSubview:_applyNameLabel];
    }
    return _applyNameLabel;
}

- (YYLabel *)receivedManNameLabel{
    if (!_receivedManNameLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.numberOfLines = 0;
        _receivedManNameLabel = label;
        [self.contentView addSubview:_receivedManNameLabel];
    }
    return _receivedManNameLabel;
}

//- (YYLabel *)copiedManLabel{
//    if (!_copiedManLabel) {
//        YYLabel *label = [[YYLabel alloc] init];
//        label.font = kContentFont;
//        label.numberOfLines = 0;
//        label.textColor = [UIColor customTextGrayBlackColor];
//        _copiedManLabel = label;
//        [self.contentView addSubview:_copiedManLabel];
//    }
//    return _copiedManLabel;
//}

- (YYLabel *)applyDateLabel{
    if (!_applyDateLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        _applyDateLabel = label;
        [self.contentView addSubview:_applyDateLabel];
    }
    return _applyDateLabel;
}

- (YYLabel *)noteLabel{
    if (!_noteLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.numberOfLines = 0;
        _noteLabel = label;
        [self.contentView addSubview:_noteLabel];
    }
    return _noteLabel;
}

@end
